package com.eddie.libray.reception;

import com.eddie.libray.LibraryApp;
import com.eddie.libray.LibraryDataSource;
import com.eddie.libray.MenuConstant;
import com.eddie.libray.entity.LoginUser;
import com.eddie.libray.service.LoginService;
import com.eddie.libray.service.RegisterService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.eddie.libray.MenuConstant.MAIN_MENU_RETURN;

/**
 * 主菜单请求接受器
 *
 * @author Eddie
 * @since 2022/09/02 09:18
 **/
public class MainReception extends AbstractReception {

    private final LoginService loginService = new LoginService();

    private final RegisterService registerService = new RegisterService();

    public MainReception(LibraryApp app) {
        super(app);
    }

    @Override
    public boolean action() {
        menu.mainMenu();
        String answer = scanner.next();
        switch (answer) {
            case "1": {
                boolean login = loginAction();
                //登录成功就结束本次循环
                if (login) {
                    return false;
                }
                break;
            }
            case "2": {
                //注册用户
                registerService.registerUser();
                break;
            }
            //如果为主菜单退出条件值则结束
            case MAIN_MENU_RETURN: {
                //在主入口接收器中如果结束了，那么就直接将状态设置为null，在app中查看状态为null时就终止程序
                app.setReceptionState(null);
                return false;
            }
            default: {
                System.out.println("该项选择不存在!");
            }
        }
        return true;
    }

    /**
     * 登陆请求处理
     */
    private boolean loginAction() {
        String answer;
        do {
            menu.loginMenu();
            answer = scanner.next();
            switch (answer) {
                case "1": {
                    LoginUser loginUser = loginService.loginUser();
                    if (loginUser != null) {
                        LibraryDataSource.loginUser = loginUser;
                        System.out.println("欢迎用户：" + LibraryDataSource.loginUser.getName() + "进入管理系统，现在时间为：" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                                .format(new Date()));
                        //设置图书馆系统的状态为用户接收器状态
                        app.setReceptionState(app.getUserReception());
                        //返回true表为登陆成功
                        return true;
                    }
                    break;
                }
                case "2": {
                    //登录完成接收返回值
                    LoginUser loginUser = loginService.loginAdmin();
                    if (loginUser != null) {
                        //设置系统维护的登陆用户信息
                        LibraryDataSource.loginUser = loginUser;
                        System.out.println("欢迎管理员：" + LibraryDataSource.loginUser.getName() + "进入管理系统，现在时间为：" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                                .format(new Date()));
                        //设置图书馆系统为管理员接收状态
                        app.setReceptionState(app.getAdminReception());
                        //返回true代表登陆成功
                        return true;
                    }
                    break;
                }
                default: {
                    break;
                }
            }
            //如果登陆菜单返回条件与输入值比对就结束
        } while (!MenuConstant.LOGIN_MENU_RETURN.equals(answer));
        return false;
    }
}
